package com.fzn.springboot_jsp_shiro.service;

import com.fzn.springboot_jsp_shiro.dao.UserDao;
import com.fzn.springboot_jsp_shiro.entity.Perms;
import com.fzn.springboot_jsp_shiro.entity.User;
import com.fzn.springboot_jsp_shiro.utils.SaltUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author fzn
 * @date 2022/3/19
 * @apiNote
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public void register(User user) {
        // 处理业务 调用dao
        // 明文的密码进行 MD5 + salt + hash 散列
        // 生成随机盐
        String salt = SaltUtils.getSalt(8);
        // 保存到数据库
        user.setSalt(salt);
        Md5Hash md5Hash = new Md5Hash(user.getPassword(),user.getSalt(),1024);
        // 设置成加密的密码
        user.setPassword(md5Hash.toHex());

        userDao.save(user);
    }

    @Override
    public User findByUserName(String username) {
        return userDao.findByUserName(username);
    }

    @Override
    public User findRoleByUserName(String username) {
        return userDao.findRoleByUserName(username);
    }

    @Override
    public List<Perms> findPermsByRoleId(String id) {
        return userDao.findPermsByRoleId(id);
    }
}
